﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLL
{
    public class DanhSachKhachHangBLL : BaseBLL
    {
        DAL.KhachHangDAO _KhachHangDAO;

        public DanhSachKhachHangBLL()
        {
            _KhachHangDAO = new DAL.KhachHangDAO();
        }

        public int CountMatchFilter(String[] filter)
        {
            return _KhachHangDAO.CountMatchFilter(filter);
        }

        public List<DTO.KhachHangDTO> MatchFilterPerPage(int page, String[] filter)
        {
            return _KhachHangDAO.MatchFilterPerPage(page, filter);
        }

        public int UpdateList(List<DTO.KhachHangDTO> dtos)
        {
            int r = -1;
            try
            {
                System.Data.SqlClient.SqlConnection connection = OpenConnection();
                System.Data.SqlClient.SqlTransaction transaction = BeginTransaction();

                foreach (DTO.KhachHangDTO dto in dtos)
                {
                    r = UpdateKhachHang(connection, transaction, dto);
                }
                Commit();
                CloseConnection();
                return r;
            }
            catch (System.Exception ex)
            {
                Rollback();
                CloseConnection();
                throw new System.Exception("UpdateList failed " + ex.Message);
            }
            finally
            {
                CloseConnection();
            }           
        }

        public int UpdateKhachHang(
            System.Data.SqlClient.SqlConnection connection,
            System.Data.SqlClient.SqlTransaction transaction,
            DTO.KhachHangDTO dto)
        {
            return _KhachHangDAO.Update(connection, transaction, dto);
        }

        public DTO.KhachHangDTO GetKhachHangByID(int IDKhachHang)
        {
            DTO.KhachHangDTO dto = new DTO.KhachHangDTO();
            dto.ID = IDKhachHang;
            _KhachHangDAO.Select(null, null, ref dto);
            return dto;
        }
    }
}
